import 'dart:io';

void platformTest() {
  print('numberOfProcessors ${Platform.numberOfProcessors}');
  print('executable ${Platform.executable}');
  print('localeName ${Platform.localeName}');
  print('localHostname ${Platform.localHostname}');
  print('operatingSystem ${Platform.operatingSystem}');
  print('operatingSystemVersion ${Platform.operatingSystemVersion}');
  print('packageConfig ${Platform.packageConfig}');
  print('version ${Platform.version}');
  print('resolvedExecutable ${Platform.resolvedExecutable}');
  print('pathSeparator ${Platform.pathSeparator}');
  print('pathSeparator ${Platform}');
}

void pathTest() {
  for (var k in Platform.environment.keys) {
    print('${k}: ${Platform.environment[k]}');
  }
  Platform.environment['Path']!.split(';').forEach(print);
}

void scriptTest() {
  print(Platform.script);
  print(Platform.script.toFilePath(windows: true));
  print(Platform.script.toFilePath(windows: false));
}

void isTest() {
  print('isAndroid ${Platform.isAndroid}');
  print('isIOS ${Platform.isIOS}');
  print('isWindows ${Platform.isWindows}');
  print('isLinux ${Platform.isLinux}');
  print('isFuchsia ${Platform.isFuchsia}');
  print('isMacOS ${Platform.isMacOS}');
}

void demoTest() async{
  final semicolon = ';'.codeUnitAt(0);
  var result = <int>[];

  final script = File(Platform.script.toFilePath());
  RandomAccessFile file = await script.open(mode: FileMode.read);

  // Deal with each byte.
  while (true) {
    final byte = await file.readByte();
    result.add(byte);
    if (byte == semicolon) {
      print(String.fromCharCodes(result));
      await file.close();
      break;
    }
  }
  result.clear();
  Stream<List<int>> stream = File(Platform.script.toFilePath()).openRead();
  await for (var data in stream) {
    for (int i = 0; i < data.length; i++) {
      result.add(data[i]);
      if (data[i] == semicolon) {
        print(String.fromCharCodes(result));
        return;
      }
    }
  }
}

void main() {
  platformTest();
  scriptTest();
  isTest();
  pathTest();
  demoTest();
}
